package cc.facesign.admin.service.impl;

import cc.facesign.admin.bean.TblConfUser;
import cc.facesign.admin.mapper.TblConfUserMapper;
import cc.facesign.admin.service.TblConfUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author testjava
 * @since 2022-05-06
 */
@Service
@Transactional
public class TblConfUserServiceImpl extends ServiceImpl<TblConfUserMapper, TblConfUser> implements TblConfUserService {

    @Override
    public void modifyStatusBatch(String code, List<String> cardIds, Integer signState) {
        baseMapper.modifySignStateBatch(code, cardIds, signState);
    }

    @Override
    public void modifyStatus(String code, String cardId, Integer signState) {
        baseMapper.modifySignState(code, cardId, signState);
    }

    @Override
    public void saveOrUpdate(List<TblConfUser> list) {
        List<TblConfUser> insertList = new ArrayList<>();
        List<TblConfUser> updateList = new ArrayList<>();

        for (TblConfUser e : list) {
            if (isExistRelate(e.getConfId(), e.getUserId())) updateList.add(e);
            else insertList.add(e);
        }

        saveBatch(insertList);
        updateIsSign(updateList);
    }

    @Override
    public void relateBatch(List<TblConfUser> list) {
        List<TblConfUser> res = new ArrayList<>();
        for (TblConfUser e : list) {
            if (!isExistRelate(e.getConfId(), e.getUserId())) {
                e.setIsSign(0);
                res.add(e);
            }
        }

        if (res.size() != 0)
            baseMapper.relateBatch(res);
    }

    @Override
    public void updateIsSign(List<TblConfUser> list) {
        for (TblConfUser confUser : list) {
            baseMapper.updateIsSign(confUser);
        }
    }

    @Override
    public Boolean isExistRelate(String confId, String userId) {
        return baseMapper.isExistRelate(confId, userId);
    }

    @Override
    public void deleteBatch(String id, List<String> ids) {
        baseMapper.deleteBatchUserIds(id, ids);
    }


}
